package com.tetra.arena.core;

import org.bukkit.entity.Player;

import com.tetra.arena.mysql.DataSource;

public class CommandHandler {
	DataSource database;

	public CommandHandler(DataSource database) {
		this.database = database;
	}

	private enum Mainargs {
		team, queue, who, info, party
	}

	private enum TeamArgs {
		create, disband, join, invite, open, leave
	}

	private enum PartyArgs {
		create, invite, join, disband, leave, kick
	}

	public void interpret(Player sender, String[] args) {
		switch (Mainargs.valueOf(args[0])) {
		case team:
			if (args.length > 1) {
				teamInterpreter(sender, args);
			}
			break;
		case party:
			if (args.length > 1) {
				partyInterpreter(sender, args);
			}
			break;
		case queue:
			break;
		case who:
			break;
		case info:
			break;
		}
	}

	public void teamInterpreter(Player sender, String[] args) {
		switch (TeamArgs.valueOf(args[1])) {
		case create:
			if (args.length > 3) {
				sender.sendMessage("This command does not accept this many arguements");
				sender.sendMessage("Usage: /arena team create <name>");
			} else if (args.length == 3) {
				database.createTeam(args[2], sender);
			}
			break;
		case disband:
			if (args.length == 2) {
				database.deleteTeam(sender);
			} else {
				sender.sendMessage("This command does not accept this many arguements");
				sender.sendMessage("Usage: /arena team disband");
			}
			break;
		case join:
			if (args.length > 3) {
				sender.sendMessage("This command does not accept this many arguements");
				sender.sendMessage("Usage: /arena team join <name>");
			} else if (args.length == 3) {
				database.joinTeam(sender, args[2]);
			}
			break;
		case invite:
			if (args.length > 3) {
				sender.sendMessage("This command does not accept this many arguements");
				sender.sendMessage("Usage: /arena team invite <name>");
			} else if (args.length == 3) {
				database.sendInvite(sender, args[2]);
			}
			break;
		case leave:
			if (args.length == 2) {
				database.leaveTeam(sender);
			} else {
				sender.sendMessage("This command does not accept this many arguements");
				sender.sendMessage("Usage: /arena team leave");
			}
			break;
		case open:
			if (args.length == 2) {
				database.setTeamOpenState(sender);
			} else {
				sender.sendMessage("This command does not accept this many arguements");
				sender.sendMessage("Usage: /arena team open");
			}
			break;
		}
	}

	public void partyInterpreter(Player sender, String[] args) {
		switch (PartyArgs.valueOf(args[1])) {
		case create:
			if (args.length > 2) {
				sender.sendMessage("This command does not accept this many arguements");
				sender.sendMessage("Usage: /arena party create");
			} else {
				database.createParty(sender);
			}
			break;
		case kick:
			if(args.length != 3){
				sender.sendMessage("This command does not accept this many arguements");
				sender.sendMessage("Usage: /arena party invite <player name>");
			} else{
				database.kickPlayerFromParty(sender,args[2]);
			}
		case join:
			if (args.length != 3) {
				sender.sendMessage("This command does not accept this many arguements");
				sender.sendMessage("Usage: /arena party kick <player name>");
			} else {
				database.joinParty(sender, args[2]);
			}
			break;
		case leave:
			if(args.length !=2){
				sender.sendMessage("This command does not accept this many arguements");
				sender.sendMessage("Usage: /arena party leave");
			} else{
				database.leaveParty(sender);
			}
			break;
		case invite:
			if (args.length == 3) {
				database.partyInvite(sender, args[2]);
			} else {
				sender.sendMessage("This command does not accept this many arguements");
				sender.sendMessage("Usage: /arena party invite <player name>");
			}
			break;
		case disband:
			if (args.length == 2) {
				database.disbandParty(sender);
			} else {
				sender.sendMessage("This command does not accept this many arguements");
				sender.sendMessage("Usage: /arena party disband");
			}
			break;
		}
	}

	public void queueInterpreter(Player sender, String[] args) {

	}

	public void whoInterpreter(Player sender, String[] args) {

	}

	public void infoInterpreter(Player sender, String[] args) {

	}

}
